package br.com.rookgraph.run;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import br.com.rookgraph.Graph;
import br.com.rookgraph.utils.ExecuteHeuristicas;

public class RunBrancheReduceMultHeuristics {
	
	private static Scanner in = new Scanner(System.in);
	
	private static List<ExecuteHeuristicas> heuristicas = new ArrayList<ExecuteHeuristicas>();
	
	private static String nameFile;
	
	public static void main(String[] args) throws CloneNotSupportedException, IOException {
		String file = getFile();
		
		getHeuristicas();
		
		executarMultHeuristicas(file);
		
	}

	private static void executarMultHeuristicas(String file) throws IOException, FileNotFoundException, CloneNotSupportedException {
		ReadFileGraph readFileGraph = new ReadFileGraph(file);
		
		Graph graph = readFileGraph.read();
		
		File fileResult = new File("resources/graphs-result/" + nameFile + "-result.csv");
		fileResult.createNewFile();
		
	    FileOutputStream writer = new FileOutputStream(fileResult);
	    OutputStreamWriter wStream = new OutputStreamWriter(writer);
	    BufferedWriter wBuffer = new BufferedWriter(wStream);
	    
	    wBuffer.append("Graph;");
	    for (ExecuteHeuristicas h : ExecuteHeuristicas.values()) {
	    	wBuffer.append(h + ";");
	    }
	    
		int i = 1;
		while (graph != null) {
			wBuffer.newLine();
			wBuffer.append("#" + i + ";");
			
//			Graph graphTree = graph.getTree(new Graph(graph.getOrder()), 1);
			
			Graph graphTree = graph.getTreeLarge(new Graph(graph.getOrder()), 1);
			System.out.println("Graph #" + i);
			Graph graphResult;
			
			for (ExecuteHeuristicas h : ExecuteHeuristicas.values()) {
				if (heuristicas.contains(h)) {
					graphResult = h.execute((Graph)graph.clone(), (Graph)graphTree.clone());
//					System.out.print(numberSpace(i) + graphResult.getBranchs().length);
					wBuffer.append(Integer.toString(graphResult.getBranchs().length));
				} else {
					wBuffer.append("--");
				}
				
				wBuffer.append(";");
			}
			
			i++;
			
			graph = readFileGraph.read();
		}
		
		wBuffer.close();
		wStream.close();
		writer.close();
	}
	
	private static String getFile() {
		String pathFile = "resources/graphs-generate/";	
		System.out.println("Nome do arquivo de grafos a serem usados: ");
		nameFile = in.nextLine();
		return pathFile + nameFile;
	}

	private static void getHeuristicas(){
		System.out.println("Digite as heuristicas que devem ser usadas: ");
		String list = in.nextLine();
		String[] chars = list.split(" ");
		
		for (String op : chars) {
			switch (op.charAt(0)) {
				case '1':
					heuristicas.add(ExecuteHeuristicas.NORMAL);
					break;
				case '2':
					heuristicas.add(ExecuteHeuristicas.ALTERADO);
					break;
				case '3':
					heuristicas.add(ExecuteHeuristicas.PLUS);
					break;
				case '4':
					heuristicas.add(ExecuteHeuristicas.PLUS2);
					break;
				case '5':
					heuristicas.add(ExecuteHeuristicas.APPROXIMATIVE);
					break;
				default:
					break;
			}
		}

	}
	
	@SuppressWarnings("unused")
	private static String numberSpace(int i) {
		String spaceDefault = "          ";
		while (i > 9 && i < 10) {	
			spaceDefault.charAt(0);
			i = i/10;
		}
		
		return spaceDefault;
	}
}
